<?php
namespace App\Controllers\Stat;

use App\Controllers\BaseController;
use App\Models\Nc;
use App\Library\Helper;

class NcController extends BaseController {

    public function initialize() {
        parent::initialize();
        if($this->request->isPost()) {
            $this->checkLogin();
        }
    }

    //---------------------------------------------------------------------------------
    //NC号码展示
    //---------------------------------------------------------------------------------
    public function indexAction() {
        $this->display('stat/nc/index');
    }

    //---------------------------------------------------------------------------------
    //获取指定的NC号码
    //---------------------------------------------------------------------------------
    public function getsAction() {
        $data = [
            'mobile' => $this->request->getPost('mobile')
        ];

        if($data['mobile'] == '') {
            return $this->sendAjax(false, '获取数据失败，未获取到指定的查询条件！');
        }
        if(!Helper::isMobile($data['mobile'])) {
            return $this->sendAjax(false, '获取数据失败，查询条件格式不正确！');
        }
        $log = new Nc();
        $ret = $log->gets($data['mobile']);
        if($ret !== false) {
            return $this->sendAjax(true, $ret);
        } else {
            return $this->sendAjax(false, "获取数据失败，系统错误！");
        }
    }


    //----------------------------------------------------------------
    //删除NC号码
    //----------------------------------------------------------------
    public function removeAction() {
        $id = $this->request->getPost('mobile') != '' ? $this->request->getPost('mobile') : $this->request->getQuery('mobile');
        $nc = new Nc();
        if($nc->remove($id)) {
            return $this->sendAjax(true, '删除成功');
        } else {
            return $this->sendAjax(false, "删除失败，系统错误！");
        }
    }

    //----------------------------------------------------------------
    // 新增NC号码
    //----------------------------------------------------------------
    public function addAction() {
        if($this->request->isGet()) {
            $this->display('/stat/nc/add');
        } else {
            $mobile       = trim($this->request->getPost('mobile'));
            $mobilesArray = explode(',', $mobile);
            $mobiles      = [];
            foreach($mobilesArray as $v) {
                $mobiles[] = trim($v);
            }

           
            $nc   = new Nc();
            $data = $nc->add($mobiles);
            if($data) {
                return $this->sendAjax(true, '新增NC号码成功！');
            } else {
                return $this->sendAjax(false, '新增NC号码失败，系统错误！');
            }
        }
    }

    //----------------------------------------------------------------
    // 多个删除NC号码
    //----------------------------------------------------------------
    public function delsAction() {
        if($this->request->isGet()) {
            $this->display('/stat/nc/dels');
        } else {
            $mobile       = trim($this->request->getPost('mobile'));
            $mobilesArray = explode(',', $mobile);
            $mobiles      = [];
            foreach($mobilesArray as $v) {
                $mobiles[] = trim($v);
            }
            $nc   = new Nc();
            $data = $nc->dels($mobiles);
            if($data) {
                return $this->sendAjax(true, '批量删除NC号码成功！');
            } else {
                return $this->sendAjax(false, '批量NC号码失败，系统错误！');
            }
        }
    }

    //----------------------------------------------------------------
    // 清空并新增NC号码
    //----------------------------------------------------------------
    public function bulkUpdateAction() {
        if($this->request->isGet()) {
            $this->display('/stat/nc/bulkUpdate');
        } else {
            $mobile       = trim($this->request->getPost('mobile'));
            $mobilesArray = explode(',', $mobile);
            $mobiles      = [];
            foreach($mobilesArray as $v) {
                $mobiles[] = trim($v);
            }
            $nc   = new Nc();
            $data = $nc->bulkUpdate($mobiles);
            if($data) {
                return $this->sendAjax(true, '清空并新增NC号码成功！');
            } else {
                return $this->sendAjax(false, '清空并新增NC号码失败，系统错误！');
            }
        }
    }
}
